<template>
  <div class="specsDialog">
    <el-dialog
      :title="isAdd ? '添加商品规格' : '修改商品规格'"
      :visible.sync="isShow"
      :before-close="cancel"
      center
    >
      <el-form ref="specsInfo" :model="form" :rules="rules">
        <el-form-item
          prop="specsname"
          label="规格名称"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.specsname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          v-for="(item, index) in attrArr"
          label="规格属性"
          :key="index"
          :label-width="formLabelWidth"
        >
          <el-input
            class="inp_info"
            v-model="item.value"
            autocomplete="off"
          ></el-input>
          <el-button v-if="index == 0" type="primary" @click="add_info"
            >新增规格属性</el-button
          >
          <el-button v-else type="danger" @click="add_remove(index)"
            >删除</el-button
          >
        </el-form-item>
        <el-form-item label="活动区域" :label-width="formLabelWidth">
          <el-switch
            v-model="form.status"
            active-color="#13ce66"
            inactive-color="gray"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
// specsadd 添加
import { specsadd, specsinfo, specsedit } from "../../request/api";
export default {
  data() {
    return {
      form: {},
      dialogFormVisible: false,
      form: {
        specsname: "",
        attrs: "",
        status: 1,
      },
      formLabelWidth: "120px",
      rules: {
        specsname: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 2, max: 8, message: "长度在 2 到 8 个字符", trigger: "blur" },
        ],
      },
      //定义一个数组  循环表单项
      attrArr: [{ value: "" }],
    };
  },
  props: ["isShow", "isAdd"],
  mounted() {},
  methods: {
    ...mapActions({
      getSpecsAction: "specs/getSpecsAction",
      getCountAction: "specs/getCountAction",
    }),
    //属性添加
    add_info() {
      if (this.attrArr.length > 4) {
        this.$message.warning("最多添加5项!!!不能再加了");
      } else {
        this.attrArr.push({
          value: "",
        });
      }
    },
    //属性删除
    add_remove(index) {
      this.attrArr.splice(index, 1);
    },
    //点击取消  隐藏dialog
    cancel() {
      this.$emit("cancel");
      this.reset();
    },
    //重置表单的值
    reset() {
      this.form = {
        specsname: "",
        attrs: "",
        status: 1,
      };
      //清空验证的方法
      this.$refs.specsInfo.resetFields();
      //清空动态列表
      this.attrArr = [{ value: "" }];
    },
    //数据回显
    lookup(id) {
      specsinfo({ id }).then((res) => {
        if (res.code == 200) {
          this.form = res.list[0];
        //   console.log(res.list[0]);
          this.form.id = id;
          //后端返回的数据格式 是数组 字符串的
          //我们想要实现回显  必须将数据格式修改为数组套对象的
          this.form.attrs.map((item, idx) => {
            if (idx == 0) {
              this.attrArr[0].value = item;
            } else {
              this.attrArr.push({
                value: item,
              });
            }
          });
        }
      });
    },
    //表单提交格式
    confirm() {
      this.$refs.specsInfo.validate((val) => {
        if (val) {
          if (this.isAdd) {
            // console.log(this.form,"表单对象");
            // console.log(this.attrArr,"tianjai");
            this.form.attrs = this.attrArr.map((item) => item.value).join(",");
            specsadd(this.form).then((res) => {
              if (res.code == 200) {
                this.$message.success(res.msg);
                //重新获取数据
                this.getSpecsAction();
                //重新获取总条数
                this.getCountAction();
                //关闭弹框
                this.cancel();
              } else {
                this.$message.success(res.msg);
              }
            });
          } else {
            specsedit(this.form).then((res) => {
              if (res.code == 200) {
                this.$message.success(res.msg);
                //重新获取数据
                this.getSpecsAction();
                //重新获取总条数
                this.getCountAction();
                //关闭弹框
                this.cancel();
              } else {
                this.$message.success(res.msg);
              }
            });
          }
        }
      });
    },
  },
};
</script>
<style scoped>
.inp_info {
  width: 78%;
}
</style>